package cn.com.enersun.iec61850.integratedDataProcessing;

import cn.com.enersun.iec61850.web.domain.DeviceList;
import cn.com.enersun.iec61850.web.domain.ServerModelStructure;
import cn.com.enersun.iec61850.web.mapper.DeviceListMapper;
import cn.com.enersun.iec61850.web.mapper.ServerModelStructureMapper;
import org.openmuc.openiec61850.ClientAssociation;
import org.openmuc.openiec61850.ServerModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YYC on 2017/3/21.
 */
@Component
public class IntegratedDataProcessing extends Thread{
    private final static Logger logger = LoggerFactory.getLogger(IntegratedDataProcessing.class);

    public static ClientDataProcessing clientDataProcessing;
    public static ServerDataProcessing serverDataProcessing;
    public static Map<String, ServerModel> clientServerModels;
    public static ServerModel serverModel;

    @Autowired
    private DeviceListMapper deviceListMapper;
    @Autowired
    private ServerModelStructureMapper serverModelStructureMapper;

    public IntegratedDataProcessing() throws IOException {
        clientServerModels = new HashMap<String, ServerModel>();
        clientDataProcessing = new ClientDataProcessing(
                clientServerModels,
                new HashMap<String, ClientAssociation>(),
                this);
        //第一步：从数据库得到需要连接的在线监测装置配置信息和ServerModel，并进行连接
//        clientDataProcessing.addClientAssociationByDatabase();
//        User user = userMapper.findByName("liuxi");
        //第三步：更新数据

        //第四步：发布服务
//        serverDataProcessing = new ServerDataProcessing(
//                new ServerNetParameter(InetAddress.getLocalHost(), 102, 0),
//                serverModel,
//                "IntegratedDataUnit",//综合数据处理单元名字
//                null
//        );
    }
    public void init() {
        //第一步：从数据库得到需要连接的在线监测装置配置信息和ServerModel，并进行连接
        //deviceListMapper.insertDevice(new DeviceList("10.111.55.126", "10.111.55.126",102));
        List<DeviceList> deviceLists = deviceListMapper.selectDevices();
        clientDataProcessing.addClientAssociationByDatabase(deviceLists);
        //第三步：更新数据

        //第四步：发布服务
    }

    public int insertDevice(DeviceList deviceList){
        return deviceListMapper.insertDevice(deviceList);
    }

    public void updateDeviceStatus(String devIp, boolean online) {
        deviceListMapper.updateDeviceStatus(devIp, online);
    }

    public void updateDevice(DeviceList deviceList) {
        deviceListMapper.updateDevice(deviceList);
    }

    public DeviceList selectDevice(String ip) {
        return deviceListMapper.selectDevice(ip);
    }

    public void deleteDevice(String ip){
        deviceListMapper.deleteDevice(ip);
    }

    public List<ServerModelStructure> getModelByDatabase(String devName, String moduleName, String nodeClass) throws Exception {
        return serverModelStructureMapper.selectModelByNodeClass(devName, moduleName, nodeClass);
    }

    public List<ServerModelStructure> getDataSetDoByDatabase(String devName) throws Exception {
        return serverModelStructureMapper.selectDataSetDoByNodeFc(devName);
    }

    public List<ServerModelStructure> getModelLeafByDatabase(String devName) throws Exception {
        return serverModelStructureMapper.selectModelLeaf(devName);
    }

    public int insertModelStructure(ServerModelStructure modelStructure){
        return serverModelStructureMapper.insertServerModelValue(modelStructure);
    }

}
